{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 前言\n",
    "\n",
    "对于 Pandas 的 DataFrame 的基本属性进行了解，比如数据格式问题造成的一些问题，就需要了解清楚之后来操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>1</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>深圳</td>\n",
       "      <td>银行</td>\n",
       "      <td>19910403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>2</td>\n",
       "      <td>万科A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>全国地产</td>\n",
       "      <td>19910129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>4</td>\n",
       "      <td>国农科技</td>\n",
       "      <td>深圳</td>\n",
       "      <td>生物制药</td>\n",
       "      <td>19910114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000005.SZ</td>\n",
       "      <td>5</td>\n",
       "      <td>世纪星源</td>\n",
       "      <td>深圳</td>\n",
       "      <td>环境保护</td>\n",
       "      <td>19901210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>6</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>区域地产</td>\n",
       "      <td>19920427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3719</th>\n",
       "      <td>688366.SH</td>\n",
       "      <td>688366</td>\n",
       "      <td>昊海生科</td>\n",
       "      <td>上海</td>\n",
       "      <td>生物制药</td>\n",
       "      <td>20191030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3720</th>\n",
       "      <td>688368.SH</td>\n",
       "      <td>688368</td>\n",
       "      <td>晶丰明源</td>\n",
       "      <td>上海</td>\n",
       "      <td>半导体</td>\n",
       "      <td>20191014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3721</th>\n",
       "      <td>688369.SH</td>\n",
       "      <td>688369</td>\n",
       "      <td>致远互联</td>\n",
       "      <td>北京</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20191031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3722</th>\n",
       "      <td>688388.SH</td>\n",
       "      <td>688388</td>\n",
       "      <td>嘉元科技</td>\n",
       "      <td>广东</td>\n",
       "      <td>元器件</td>\n",
       "      <td>20190722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3723</th>\n",
       "      <td>688389.SH</td>\n",
       "      <td>688389</td>\n",
       "      <td>普门科技</td>\n",
       "      <td>深圳</td>\n",
       "      <td>医疗保健</td>\n",
       "      <td>20191105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3724 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code  symbol  name area industry  list_date\n",
       "0     000001.SZ       1  平安银行   深圳       银行   19910403\n",
       "1     000002.SZ       2   万科A   深圳     全国地产   19910129\n",
       "2     000004.SZ       4  国农科技   深圳     生物制药   19910114\n",
       "3     000005.SZ       5  世纪星源   深圳     环境保护   19901210\n",
       "4     000006.SZ       6  深振业A   深圳     区域地产   19920427\n",
       "...         ...     ...   ...  ...      ...        ...\n",
       "3719  688366.SH  688366  昊海生科   上海     生物制药   20191030\n",
       "3720  688368.SH  688368  晶丰明源   上海      半导体   20191014\n",
       "3721  688369.SH  688369  致远互联   北京     软件服务   20191031\n",
       "3722  688388.SH  688388  嘉元科技   广东      元器件   20190722\n",
       "3723  688389.SH  688389  普门科技   深圳     医疗保健   20191105\n",
       "\n",
       "[3724 rows x 6 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "\n",
    "# 读取股票样例数据\n",
    "df = pd.read_csv('stock_basic.csv',index_col='Unnamed: 0')\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基础属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3724, 6)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape # 行数 列数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ts_code      object\n",
       "symbol        int64\n",
       "name         object\n",
       "area         object\n",
       "industry     object\n",
       "list_date     int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes # 列数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.ndim # 数据维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,\n",
       "            ...\n",
       "            3714, 3715, 3716, 3717, 3718, 3719, 3720, 3721, 3722, 3723],\n",
       "           dtype='int64', length=3724)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index # 行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['ts_code', 'symbol', 'name', 'area', 'industry', 'list_date'], dtype='object')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns # 列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['000001.SZ', 1, '平安银行', '深圳', '银行', 19910403],\n",
       "       ['000002.SZ', 2, '万科A', '深圳', '全国地产', 19910129],\n",
       "       ['000004.SZ', 4, '国农科技', '深圳', '生物制药', 19910114],\n",
       "       ...,\n",
       "       ['688369.SH', 688369, '致远互联', '北京', '软件服务', 20191031],\n",
       "       ['688388.SH', 688388, '嘉元科技', '广东', '元器件', 20190722],\n",
       "       ['688389.SH', 688389, '普门科技', '深圳', '医疗保健', 20191105]],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values # 对象值，二位 ndarray 数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 整体情况查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>1</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>深圳</td>\n",
       "      <td>银行</td>\n",
       "      <td>19910403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>2</td>\n",
       "      <td>万科A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>全国地产</td>\n",
       "      <td>19910129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>4</td>\n",
       "      <td>国农科技</td>\n",
       "      <td>深圳</td>\n",
       "      <td>生物制药</td>\n",
       "      <td>19910114</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code  symbol  name area industry  list_date\n",
       "0  000001.SZ       1  平安银行   深圳       银行   19910403\n",
       "1  000002.SZ       2   万科A   深圳     全国地产   19910129\n",
       "2  000004.SZ       4  国农科技   深圳     生物制药   19910114"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3) # 显示头部几行，默认 5 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3721</th>\n",
       "      <td>688369.SH</td>\n",
       "      <td>688369</td>\n",
       "      <td>致远互联</td>\n",
       "      <td>北京</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20191031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3722</th>\n",
       "      <td>688388.SH</td>\n",
       "      <td>688388</td>\n",
       "      <td>嘉元科技</td>\n",
       "      <td>广东</td>\n",
       "      <td>元器件</td>\n",
       "      <td>20190722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3723</th>\n",
       "      <td>688389.SH</td>\n",
       "      <td>688389</td>\n",
       "      <td>普门科技</td>\n",
       "      <td>深圳</td>\n",
       "      <td>医疗保健</td>\n",
       "      <td>20191105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code  symbol  name area industry  list_date\n",
       "3721  688369.SH  688369  致远互联   北京     软件服务   20191031\n",
       "3722  688388.SH  688388  嘉元科技   广东      元器件   20190722\n",
       "3723  688389.SH  688389  普门科技   深圳     医疗保健   20191105"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(3) # 显示末尾几行，默认 5 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 3724 entries, 0 to 3723\n",
      "Data columns (total 6 columns):\n",
      "ts_code      3724 non-null object\n",
      "symbol       3724 non-null int64\n",
      "name         3724 non-null object\n",
      "area         3722 non-null object\n",
      "industry     3722 non-null object\n",
      "list_date    3724 non-null int64\n",
      "dtypes: int64(2), object(4)\n",
      "memory usage: 363.7+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info() # 相关信息概览：行数、列数、列索引、列非空值个数、列类型、内存占用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3724.000000</td>\n",
       "      <td>3.724000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>314674.658432</td>\n",
       "      <td>2.008114e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>267924.905904</td>\n",
       "      <td>8.152001e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.990121e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2477.500000</td>\n",
       "      <td>2.000112e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>300472.500000</td>\n",
       "      <td>2.010081e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>600739.250000</td>\n",
       "      <td>2.016031e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>688389.000000</td>\n",
       "      <td>2.019113e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              symbol     list_date\n",
       "count    3724.000000  3.724000e+03\n",
       "mean   314674.658432  2.008114e+07\n",
       "std    267924.905904  8.152001e+04\n",
       "min         1.000000  1.990121e+07\n",
       "25%      2477.500000  2.000112e+07\n",
       "50%    300472.500000  2.010081e+07\n",
       "75%    600739.250000  2.016031e+07\n",
       "max    688389.000000  2.019113e+07"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe() # 快速综合统计结果：计数、均值、标准差、最大值、四分位数、最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
